package com.cuput.partybuilding.contact.adapter;

import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.cuput.partybuilding.contact.R;
import com.cuput.partybuilding.contact.bean.Group;
import com.cuput.partybuilding.publiclibrary.views.PinnedHeaderItemDecoration;

import java.util.ArrayList;

import retrofit2.http.PUT;

/**
 * Created by wei on 2017-05-08.
 */

public class ContactGroupAdapter extends RecyclerView.Adapter<ContactGroupAdapter.ContactGroupVH> implements PinnedHeaderItemDecoration.PinnedHeaderAdapter {
    private Context mContext;
    private ArrayList<Group> mGroups;
    private RecyclerView mParentRecycleView;

    public void setGroups(Context context, ArrayList<Group> groups, RecyclerView recyclerView) {
        this.mContext = context;
        this.mGroups = groups;
        this.mParentRecycleView = recyclerView;
    }

    @Override
    public ContactGroupVH onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.layout_group, parent, false);
        ContactGroupVH contactGroupVH = new ContactGroupVH(view);
        contactGroupVH.setViewType(viewType);
        return contactGroupVH;
    }

    @Override
    public void onBindViewHolder(final ContactGroupVH holder, final int position) {
        final Group group = mGroups.get(position);
        holder.groupName.setText(group.getGroupName());
        holder.contentCount.setText(group.getContacts().size() + "条");

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mContext);
        linearLayoutManager.setStackFromEnd(true);
        holder.mRecyclerView.setLayoutManager(linearLayoutManager);
        holder.mRecyclerView.setHasFixedSize(true);

        ContactGroupItemAdapter contactGroupItemAdapter = new ContactGroupItemAdapter(mContext, group.getContacts());
        holder.mRecyclerView.setAdapter(contactGroupItemAdapter);

        holder.groupHeader.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (! holder.isShowGroupItem) {
                    holder.mRecyclerView.setVisibility(View.VISIBLE);
                    holder.isShowGroupItem = true;
                    mParentRecycleView.scrollToPosition(position);
                } else {
                    holder.mRecyclerView.setVisibility(View.GONE);
                    holder.isShowGroupItem = false;
                }
            }
        });
    }

    @Override
    public int getItemViewType(int position) {
        return super.getItemViewType(position);
    }

    @Override
    public int getItemCount() {
        return mGroups == null ? 0 : mGroups.size();
    }

    @Override
    public boolean isPinnedViewType(int viewType) {
        return true;
    }

    static class ContactGroupVH extends RecyclerView.ViewHolder {
        public RelativeLayout groupHeader;
        public TextView groupName;
        public TextView contentCount;
        public RecyclerView mRecyclerView;
        public boolean isShowGroupItem = true;
        private int viewType;

        public ContactGroupVH(View view) {
            super(view);
            groupHeader = (RelativeLayout) view.findViewById(R.id.groupHeader);
            groupName = (TextView) view.findViewById(R.id.groupName);
            contentCount = (TextView) view.findViewById(R.id.contentCount);
            mRecyclerView = (RecyclerView) view.findViewById(R.id.group);
        }

        public int getViewType() {
            return viewType;
        }

        public void setViewType(int viewType) {
            this.viewType = viewType;
        }
    }

    public ArrayList<Group> getGroups() {
        return mGroups;
    }
}
